////////////////////////////////////////////////////////////////////////////////
//
//	Original file Name: InputDeviceKeyBoard.java
//	Originating Author: JWhitefield
//	
//	Description: Keyboard input device 
//
////////////////////////////////////////////////////////////////////////////////

package com.jjw.game.input;

import net.java.games.input.Component.Identifier.Key;
import net.java.games.input.Controller;
import net.java.games.input.Keyboard;

/**
 * Keyboard input device
 * 
 * @author JWhitefield
 *
 */
public class InputDeviceKeyboard extends InputDevice {
	
	//--------------------------------------------------------------------------
	//
	//Properties
	//
	//--------------------------------------------------------------------------
		
	//----------------------------------
	// Public
	//----------------------------------
	
	//----------------------------------
	//  Getters / Setters
	//----------------------------------
	
	//----------------------------------
	// Private
	//----------------------------------
	
	/**
	 * Keyboard controller
	 */
	private Keyboard keyboard;

	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	// Public
	//----------------------------------

	InputDeviceKeyboard( Controller keyboard ) {

		//Set the passed in keyboard
		this.keyboard = (Keyboard) keyboard;
	}

	
	//----------------------------------
	// Private
	//----------------------------------

	/**
	* Read the key inputs
	*/
	private void readKeys() {

		//Directional Keys
		
		//Check if any of the up keys are being pressed
		if( this.isKeyDown( Key.UP ) || this.isKeyDown( Key.W ) || this.isKeyDown( Key.NUMPAD8 ) )
			this.getUpInputButton().toggle(true);
		else 
			this.getUpInputButton().toggle(false);
		
		//Check if any of the down keys are down
		if( this.isKeyDown( Key.DOWN ) || this.isKeyDown( Key.S ) || this.isKeyDown( Key.NUMPAD5 ) )
			this.getDownInputButton().toggle(true);
		else 
			this.getDownInputButton().toggle(false);
			
		//Check if any of the left keys are down
		if( this.isKeyDown( Key.LEFT ) || this.isKeyDown( Key.A ) || this.isKeyDown( Key.NUMPAD4 ) )
			this.getLeftInputButton().toggle(true);
		else
			this.getLeftInputButton().toggle(false);
		
		//Check if any of the right keys are down
		if( this.isKeyDown( Key.RIGHT ) || this.isKeyDown( Key.D ) || this.isKeyDown( Key.NUMPAD6 ) )
			this.getRightInputButton().toggle(true);
		else 
			this.getRightInputButton().toggle(false);
	
		//Player Keys
	
		//Check if any of the attack keys are down
		if( this.isKeyDown( Key.SPACE ) || this.isKeyDown( Key.LCONTROL ) || this.isKeyDown( Key.NUMPADENTER ) )
			this.getAttackInputButton().toggle(true);
		else
			this.getAttackInputButton().toggle(false);
	
		//Menu Keys
	
		//Check if any of the menu keys are down
		if( this.isKeyDown( Key.RETURN ) )
			this.getSelectInputButton().toggle(true);
		else 
			this.getSelectInputButton().toggle(false);
	}
	
	/**
	* Is the key passed in down
	*
	*@param key the identifier for that key
	*
	*@return	the is down state of that key
	*/
	private boolean isKeyDown( Key key ) {

		//Check we have a keyboard
		if( this.keyboard == null )
			return false;
	
		//Check if the key is down or not
		return this.keyboard.isKeyDown( key );
	}
	
	//----------------------------------
	// Overridden / Implemented
	//----------------------------------
	
	@Override
	/**
	* Ticks the input device over
	*/
	public void tick() {

		//Poll the controller
		this.keyboard.poll();
	
		//Read the keys current states
		this.readKeys();
	}
}
